<?php
    include("include/header.inc.php");
    include("include/func.inc.php");
    $_SESSION['page'] = 'event';
    
    include("include/sendMail.inc.php");
    
    //Function to get detail of a theme
    function get_detail_theme($rfc, $id_theme){
        //-- FUNCTION MODULE: Z_FM_M646_GET_THEMES --
        //We must know if the function really exists
        $fce_get_themes = saprfc_function_discover($rfc, "Z_FM_M646_GET_THEMES");
        if(!$fce_get_themes){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }

        //Pass import parameters
        saprfc_import ($fce_get_themes,"IV_THEME",$id_theme);

        //Pass table parameters
        saprfc_table_init ($fce_get_themes,"TAB_DETAILS_THEME");

        //Call and execute the function
        $rc_get_themes = saprfc_call_and_receive ($fce_get_themes);            
        if ($rc_get_themes != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_get_themes));
            else
                echo ("Call error: ".saprfc_error($fce_get_themes));
            exit;
        }

        //Fetch the data from the internal tables
        $data_row = saprfc_table_rows ($fce_get_themes,"TAB_DETAILS_THEME");

        if ($data_row > 0)//If the theme exist
            $DATARow = saprfc_table_read ($fce_get_themes,"TAB_DETAILS_THEME",1);

        //realease the function
        saprfc_function_free($fce_get_themes);

        return $DATARow;
    }
    
    //Function to do a new registration
    function newRegistration($rfc, $dateSAPFormat, $status, $id_theme, $username){
        
        //-- FUNCTION MODULE: Z_FM_M646_REGISTRATION_NEW --
        //We must know if the function really exists
        $fce_registration_new = saprfc_function_discover($rfc, "Z_FM_M646_REGISTRATION_NEW");
        if(!$fce_registration_new){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }

        //Pass import parameters
        saprfc_import ($fce_registration_new,"IV_REGISTRATION", array ("REGISTRATION_DATE"=>$dateSAPFormat,
                                                                "REGISTRATION_STATUS"=>$status, 
                                                                "ID_THEME"=>$id_theme, 
                                                                "USERNAME"=>$username));

        //Call and execute the function
        $rc_registration_new = saprfc_call_and_receive ($fce_registration_new);            
        if ($rc_registration_new != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_registration_new));
            else
                echo ("Call error: ".saprfc_error($fce_registration_new));
            exit;
        }

        //realease the function
        saprfc_function_free($fce_registration_new);
    }
    
    //Function to set the regitration status
    function set_registration_status($rfc, $id_theme, $status, $dateSAPFormat, $username){
        //-- FUNCTION MODULE: Z_FM_M646_REGISTRATION_STATUS --
        //We must know if the function really exists
        $fce_registration_status = saprfc_function_discover($rfc, "Z_FM_M646_REGISTRATION_STATUS");
        if(!$fce_registration_status){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }

        //Pass import parameters
        saprfc_import ($fce_registration_status,"IV_ID_THEME", $id_theme);
        saprfc_import ($fce_registration_status,"IV_STATUS", $status);
        saprfc_import ($fce_registration_status,"IV_REGISTRATION_DATE", $dateSAPFormat);
        saprfc_import ($fce_registration_status,"IV_ID_MEMBER", $username);

        //Call and execute the function
        $rc_registration_status = saprfc_call_and_receive ($fce_registration_status);            
        if ($rc_registration_status != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_registration_status));
            else
                echo ("Call error: ".saprfc_error($fce_registration_status));
            exit;
        }
        //realease the function
        saprfc_function_free($fce_registration_status);
    }
    
    echo '<div data-role="content">';
    //Title
    echo '<h2>'. $texts['confirmation'][$_SESSION['lang']] .'</h2>';
    echo '<div class="tableauPage">';

    //Array to stock all themes
    $themes_array = array();

    //Array for guest if already registered
    $themes_registered_array = array();

    $indice = 0;
    for($i = 0; $i < sizeof($_SESSION['array_themes_status_initial']); ++$i){
        if($_SESSION['array_themes_status_initial'][$i][1] == 'n' || $_SESSION['array_themes_status_initial'][$i][1] == 'c'){
            if($_SESSION['array_themes_status_will'][$i][1] == 'r' || $_SESSION['array_themes_status_will'][$i][1] == 'w'){
                $themes_array[$indice][0] = $_SESSION['array_themes_status_will'][$i][0];
                $themes_array[$indice][1] = $_SESSION['array_themes_status_will'][$i][1];
                $themes_array[$indice][2] = $_SESSION['array_themes_status_initial'][$i][1];
                $indice++;
            }
        }
    }

    $indice = 0;
    for($i = 0; $i < sizeof($_SESSION['array_themes_status_initial']); ++$i){
        if($_SESSION['array_themes_status_initial'][$i][1] == 'r' || $_SESSION['array_themes_status_initial'][$i][1] == 'w'){
            $themes_registered_array[$indice] = $_SESSION['array_themes_status_initial'][$i][0];
            $indice++;
        }
    }
    
    //Empty
    $_SESSION['array_themes_status_will'] = '';
    $_SESSION['array_themes_status_initial'] = '';
      
    //Username of the current user
    if($_SESSION['login_guest'] <> '')
        $username = strtoupper($_SESSION['login_guest']);
    else
        $username = strtoupper($_SESSION['login']);

    //Get today date for the new registration
    $date = getdate();
    $month = $date[mon];
    $day = $date[mday];

    //Set format of the date
    if($date[mon] < 10)
        $month = '0'.$date[mon];
    if($date[mday] < 10)
        $day = '0'.$date[mday];

    //Format to insert in SAP
    $dateSAPFormat = $date[year].$month.$day;

    //Variable to stock the status of the new registration ('r' or 'w')
    $status = '';

    //Variable to stock the confirmation message
    $confirmationMessage = '';

    //SAP connection
    $rfc = connectSAP();

    //Foreach themes
    for($i = 0; $i < sizeof($themes_array); ++$i){        
        $id_theme = $themes_array[$i][0];
        $status = $themes_array[$i][1];
        $previous_status = $themes_array[$i][2];

        $DATARow = get_detail_theme($rfc, $id_theme);

        if($previous_status == 'n')
            newRegistration($rfc, $dateSAPFormat, $status, $id_theme, $username); 
        else if($previous_status == 'c')
            //Only set status
            set_registration_status($rfc, $id_theme, $status, $dateSAPFormat, $username);

        // Message to confirm the registration

        //Variables to display the date in a correct format
        $dateStart = substr($DATARow['STARTDATE'],-2).'.'.substr($DATARow['STARTDATE'],4,2).'.'.substr($DATARow['STARTDATE'],0,4);
        $dateEnd = substr($DATARow['ENDDATE'],-2).'.'.substr($DATARow['ENDDATE'],4,2).'.'.substr($DATARow['ENDDATE'],0,4);
        $timeStart = substr($DATARow['STARTTIME'],0,2).':'.substr($DATARow['STARTTIME'],2,2);
        $timeEnd = substr($DATARow['ENDTIME'],0,2).':'.substr($DATARow['ENDTIME'],2,2);

        if($i == 0){//Display the event title
            $confirmationMessage.= '<h2>'.$texts['an_event'][$_SESSION['lang']].' '.$DATARow['EVENT'].'</h2>';
            echo '<h1>'. $texts['an_event'][$_SESSION['lang']] .' '.$DATARow['EVENT'].'</h1>';

            if($_SESSION['login_guest'] <> '' && sizeof($themes_registered_array) > 0){
                $confirmationMessage.= '<h2>'.$texts['already_registered_to_the_theme'][$_SESSION['lang']].'</h2>';
                echo '<h2>'.$texts['already_registered_to_the_theme'][$_SESSION['lang']].'</h2>';
                $confirmationMessage.= '<div style="margin-left:10px;">';
                echo '<div style="margin-left:10px;">';

                for($a = 0; $a < sizeof($themes_registered_array); ++$a){
                    $data = get_detail_theme($rfc, $themes_registered_array[$a]);
                    $confirmationMessage.= '<p>'.$data['TITLE_THEME'].'<p>';
                    echo '<p>'.$data['TITLE_THEME'].'</br><p>';
                }
                $confirmationMessage.= '</div>';
                echo '</div>';

            }
            $confirmationMessage.= '<h2>'.$texts['registered_to_the_theme'][$_SESSION['lang']].'</h2>';
            echo '<h2>'.$texts['registered_to_the_theme'][$_SESSION['lang']].'</h2>';
        }
          
        $confirmationMessage.= '<h3>'.$texts['theme'][$_SESSION['lang']].' '.$DATARow['TITLE_THEME'].'</h3>';

        $confirmationMessage.= '<div style="margin-left:10px;">';
            //Description
            $confirmationMessage.= $DATARow['DESCRIPTION'].'<br/>';
            //Agenda
            $confirmationMessage.= $texts['agenda'][$_SESSION['lang']].' : '.$DATARow['AGENDA'].'<br/>';
            //Instructor
            $confirmationMessage.= $texts['instructor'][$_SESSION['lang']].' : '.$DATARow['INSTRUCTOR_FIRSTNAME'].' ';
            $confirmationMessage.= $DATARow['INSTRUCTOR_NAME'].'<br/>';
            //Language
            $confirmationMessage.= $texts['language'][$_SESSION['lang']].' : '.$DATARow['ID_LANGUAGE'].'<br/><br/>';
            //Dates
            $confirmationMessage.= $texts['start_date'][$_SESSION['lang']].' '.$dateStart;
            $confirmationMessage.= ' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeStart.'<br/>';
            $confirmationMessage.= $texts['end_date'][$_SESSION['lang']].' '.$dateEnd;
            $confirmationMessage.= ' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeEnd.'<br/>';
            //Locality
            $confirmationMessage.= '<b>'.$texts['locality'][$_SESSION['lang']].'</b><br/>';
            $confirmationMessage.= $texts['room'][$_SESSION['lang']].' '.$DATARow['ROOM'].'<br/>';
            $confirmationMessage.= $DATARow['ADDRESS'].'<br/>';
            $confirmationMessage.= $DATARow['ZIPCODE'].' ';
            $confirmationMessage.= $DATARow['CITY'].'<br/>';
            $confirmationMessage.= $DATARow['COUNTRY'].'<br/><br/>';
                    
        echo '<h3>'.$texts['theme'][$_SESSION['lang']].' '.$DATARow['TITLE_THEME'].'</h3>';

        echo '<div style="margin-left:10px;">';
        echo $texts['start_date'][$_SESSION['lang']].' '.$dateStart.' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeStart.'<br/>';
        echo $texts['end_date'][$_SESSION['lang']].' '.$dateEnd.' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeEnd.'<br/>';

        if($status == 'w'){//In waiting list (Status: Waiting)
            $confirmationMessage.= '<b>'.$texts['waiting_to_the_theme'][$_SESSION['lang']].'</b>';
            echo '<b>'.$texts['waiting_to_the_theme'][$_SESSION['lang']].'</b>';
        }
        echo '</div>'; 
        $confirmationMessage.= '</div>'; 
    }
    echo '</br><hr align="center" width="75%" size=1 color="red" />';
    echo '<h3 align="center">'.$texts['thanks'][$_SESSION['lang']].'</h3>';

    $mail_recipient='';
    if($_SESSION['login_guest'] <> '')
        $mail_recipient = strtoupper($_SESSION['login_guest']);
    else
        $mail_recipient = $_SESSION['mail'];

    sendMail($confirmationMessage,$mail_recipient,'');
    echo '<p align="center"><a href="./index.php" data-role="button" data-inline="true">'.$texts['home'][$_SESSION['lang']].'</a></p>';

    //close the connection
    saprfc_close($rfc);
 
    echo '</div></div>';
    include('include/footer.inc.php');  //Call footer
?>
